package com.fztx.service;

import com.spring.BeanPostProcessor;
import com.spring.Component;

import java.lang.reflect.Field;

/**
 * @author fanzitianxing
 * @title: FztxValueBeanPostProcessor
 * @projectName write-spring
 * @description: TODO
 * @date 2021/9/722:46
 */
@Component
public class FztxValueBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        for (Field field : bean.getClass().getDeclaredFields()) {
            if(field.isAnnotationPresent(FztxValue.class)){
                FztxValue fztxValue = field.getAnnotation(FztxValue.class);
                String value = fztxValue.value();
                field.setAccessible(true);
                try {
                    field.set(bean,value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return bean;
    }
}
